package com.lengyue.imakiba.logic;

import com.jfinal.plugin.activerecord.Page;
import com.lengyue.imakiba.data.Anime;

public class AnimeLogic {
	/**
	 * 根据状态取动漫,根据剧集时间倒序排列
	 * @param state 动漫状态
	 * @param num 要取的个数
	 * @return 符合条件的动漫
	 */
	public static Page<Anime> getAnimeByState(int page,int length,String state)
	{
		String select = "select DISTINCT a.*";
		String sqlExceptSelect = "from anime a left join episode e on e.anime_id = a.id";
		if(state!=null)
		{
			return Anime.dao.paginate(page, length,select,sqlExceptSelect+" where a.state = ? order by date desc",state);
		}else
		{
			return Anime.dao.paginate(page, length,select,sqlExceptSelect+" order by date desc");
		}
	}
	/**
	 * 根据类型分页查询动漫，根据Id倒序排列
	 * @param page 页号
	 * @param length 每页个数
	 * @param typeCode 动漫类型
	 * @return 指定页的动漫数据
	 */
	public static Page<Anime> getAnimeByTypeCode(int page,int length,String typeCode)
	{
		String select = "select DISTINCT a.* ";
		String sqlExceptSelect = "from anime a "
				+"INNER JOIN anime_type at on a.id = at.anime_id "
				+"where at.type_code = ? order by a.id desc";
		return Anime.dao.paginate(page, length, select, sqlExceptSelect, typeCode);
	}
	/**
	 * 根据查询条件分页查询动漫，根据Id倒序排列
	 * @param page 页号
	 * @param length 每页个数
	 * @param search 查询条件
	 * @return 指定页的动漫数据
	 */
	public static Page<Anime> getAnimeBySearch(int page,int length,String search)
	{
		String select = "select * ";
		String sqlExceptSelect = "from anime where code like CONCAT('%',?,'%') or name like CONCAT('%',?,'%') order by id desc";
		return Anime.dao.paginate(page, length, select, sqlExceptSelect,search,search);
	}
	/**
	 * 根据声优Id分页查询动漫，根据Id倒序排列
	 * @param page 页号
	 * @param length 每页个数
	 * @param cvId 声优Id
	 * @return 指定页的动漫数据
	 */
	public static Page<Anime> getAnimeByCV(int page,int length,int cvId)
	{
		return Anime.dao.paginate(page, length,"select a.* ","from anime a inner join anime_cv ac on a.id = ac.anime_id where cv_id = ? order by a.id desc",cvId);
	}
	/**
	 * 根据动漫Id分页查询类型相同的动漫
	 * @param page 页号
	 * @param length 每页个数
	 * @param animeId 动漫Id
	 * @return 符合条件的动漫数据
	 */
	public static Page<Anime> getSimilarAnimes(int page,int length,int animeId)
	{
		String select = "select DISTINCT a.*";
		String sqlExceptSelect = "from anime a LEFT JOIN episode e on a.id = e.anime_id "
				+"where a.id in ("
				+"SELECT at1.anime_id from anime_type at1 where at1.type_code in "
				+"(select at.type_code from anime_type at where at.anime_id = ?)"
				+") and a.id <> ? order by e.date desc";
		return Anime.dao.paginate(page, length, select, sqlExceptSelect, animeId,animeId);
	}
	/**
	 * 根据动漫code，动漫name分页查询动漫
	 * @param page 页号
	 * @param length 每页个数
	 * @param code 动漫code
	 * @param name 动漫name
	 * @return 符合条件的动漫数据
	 */
	public static Page<Anime> getAnimeByCodeAndName(int page,int length,String code,String name)
	{
		return Anime.dao.paginate(page, length, "select DISTINCT a.*","from anime a left join episode e on e.anime_id = a.id where a.code like CONCAT('%',?,'%') and a.name like CONCAT('%',?,'%') order by e.date desc",code,name);
	}
}
